<?php
class site extends Mod
{
	public $conn;
	
	public function __construct()
	{
		$this->conn = new Sqlite();
		$this->conn->connection($GLOBALS['dance_run']['db_name']);
	}
	
	public function index()
	{
		$status = isset($_GET['s']) ? Unit::safe($_GET['s']) : -1;
		$data['status'] = $status;
		$this->render('index', $data);
		
	}
	
	public function entry()
	{
		$id = isset($_GET['id']) ? Unit::safe($_GET['id']) : 0;
		$data = [];
		$queryData = getRow('user', ' and `id`=' . $id);
		if (is_array($queryData) && count($queryData)) {
			$data = $queryData;
		}
		$this->render('entry', $data);
	}
	
	public function table()
	{
		$this->render('table');
	}
	
	public function signin()
	{
		$this->render('signin');
	}
	
	public function m_signin()
	{
		$this->render('msignin');
	}
	
	public function search()
	{
		$keywords = isset($_GET['k']) ? Unit::safe($_GET['k']) : 'camelliae';
		$data['keywords'] = $keywords;
		$this->render('search', $data);
	}
	
	public function seat()
	{
		$this->render('seat');
	}
	
	
	
/*  	public function create_token()
	{
		$i = 0;
		while($i < 300) {
			$i ++;
			//$sqlstr = "insert into user (add_time, token) values (" . time() . ",'" . rand(1001, 9998) . "')";
			$sqlstr = "insert into code (add_time) values (" . time() . ")";
			$this->conn->sql = $sqlstr;
			$res = $this->conn->execute();
			var_dump($res);
		}
		
		
	}
	
	public function generate_token()
	{
		$res = [];
		$this->conn->sql = "select * from code";
		$data = $this->conn->rows();
		foreach($data as $key => $item) {
			$id = $item['id'];
			$time = $item['add_time'];
			$token = 'kml://' . substr(strtoupper(md5(md5($id . '_' . $time))), 0, 10);
			$sqlstr = "update code set code='$token' where id=$id";
			$this->conn->sql = $sqlstr;
			$res[] = $this->conn->execute();
			
		}
		
		
		var_dump($res);
	} */
	
	public function get_list()
	{
		$status = isset($_GET['s']) ? Unit::safe($_GET['s']) : -1;
		$keywords = isset($_GET['k']) ? Unit::safe($_GET['k']) : '';
		$whereStr = '';
		if ($status > -1) {
			$whereStr .= " AND `status`='$status'";
		}
		if ( ! empty($keywords)) {
			$whereStr .= " AND (`name` LIKE '%$keywords%' OR `inviter` LIKE '%$keywords%' OR `mobile` LIKE '%$keywords%')";
		}
		$this->conn->sql = "SELECT * FROM user WHERE 1=1" . $whereStr;
		$queryData = $this->conn->rows();
		$data = [];
		if (count($queryData)) {
			foreach ($queryData as $item) {
				$tmp = $item;
				$tmp['table_name'] = $item['table_id'] == 0 ? '待安排' : getRow('table', ' and id=' . $item['table_id'])['name'];
				$data[] = $tmp;
			}
		}
		$count = $this->conn->count();
		$res = ['code' => 0, 'msg' => '成功', 'count' => $count];
		$res['data'] = $data;
		die(json_encode($res));
	}
	
	public function get_profile()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$token = isset($_POST['t']) ? Unit::safe($_POST['t']) : 1000;
		if (is_numeric($token) && $token > 1000 && $token < 10000) {
			$sqlstr = "SELECT * FROM user WHERE token='$token'";
			$this->conn->sql = $sqlstr;
			$queryData = $this->conn->row();
			$res['code'] = 1;
			$res['msg'] = '成功';
			$res['data'] = $queryData;
		}
		die(json_encode($res));
	}
	
	public function save()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$table = isset($_POST['table']) ? $_POST['table'] : 'user';
		$id = $_POST['id'];
		$field = $_POST['field'];
		$value = $_POST['value'];
		$sets = '';
		/*$sets = ",edit_time='" . time() . "'";*/
		if ($table == 'user' && $field == 'status' && $value == 1) {
			$sets .= ",confirm_time='" . time() . "'";
		}
		$sqlstr = "UPDATE `$table` SET `$field`='$value'" . $sets . " WHERE id=$id";
		//$res['sql'] = $sqlstr;
		$this->conn->sql = $sqlstr;
		$exec = $this->conn->execute();
		$res['code'] = $exec['code'];
		$res['msg'] = $exec['msg'];
		die(json_encode($res));
	}
	
	public function add_user()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$data = $_POST;
		$name = isset($data['name']) ? $data['name'] : '来宾';
		$token = isset($data['token']) ? $data['token'] : 'kml://';
		$mobile = isset($data['mobile']) ? $data['mobile'] : '-';
		//$table_id = isset($data['table']) ? $data['table'] : '0';
		$inviter = isset($data['inviter']) ? $data['inviter'] : '卡枚连';
		$this->conn->sql = "SELECT * FROM `user` WHERE `token`='$token'";
		if ($this->conn->count() == 0) {
			$time = time();
			$this->conn->sql = "INSERT INTO `user` (`name`,`token`,`mobile`,`inviter`,`add_time`) VALUES ('$name','$token','$mobile','$inviter','$time')";
			$exec = $this->conn->execute();
			$res['code'] = $exec['code'];
			$res['msg'] = $exec['msg'];
		} else {
			$res['code'] = 2;
		}
		
		die(json_encode($res));
	}
	
	public function edit_user()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$data = $_POST;
		$id = isset($data['id']) ? $data['id'] : 0;
		$name = isset($data['name']) ? $data['name'] : '来宾';
		$token = isset($data['token']) ? $data['token'] : 'kml://';
		$mobile = isset($data['mobile']) ? $data['mobile'] : '-';
		$inviter = isset($data['inviter']) ? $data['inviter'] : '卡枚连';
		$this->conn->sql = "SELECT `token` FROM `user` WHERE `id`=$id";
		$user = $this->conn->row();
		$user_token = $user['token'];
		$number = 0;
		if ($token != $user_token) {
			$this->conn->sql = "SELECT * FROM `user` WHERE `token`='$token'";
			$number = $this->conn->count();
		}
		//$table_id = isset($data['table']) ? $data['table'] : '0';
		
		if ($number == 0 && $id > 0) {
			$time = time();
			$this->conn->sql = "UPDATE `user` SET `name`='$name',`token`='$token',`mobile`='$mobile',`inviter`='$inviter',`edit_time`='$time' WHERE `id`=$id";
			$exec = $this->conn->execute();
			$res['code'] = $exec['code'];
			$res['msg'] = $exec['msg'];
		} else {
			$res['code'] = 2;
		}
		
		die(json_encode($res));
	}
	
	public function table_users()
	{
		$res = [];
		$id = isset($_POST['id']) ? $_POST['id'] : 0;
		if ($id == 0) {
			$sqlstr = "SELECT * FROM user ORDER BY table_id ASC";
		} else {
			$sqlstr = "SELECT * FROM `user` WHERE `table_id`=$id ORDER BY table_id ASC";
		}
		
		$this->conn->sql = $sqlstr;
		$queryData = $this->conn->rows();
		foreach ($queryData as $item) {
			$tmp['value'] = $item['id'];
			$tmp['title'] = $item['name'];
			$tmp['table_id'] = $item['table_id'];
			$tmp['table_name'] = $item['table_id'] == 0 ? '待安排' : getRow('table', ' and id=' . $item['table_id'])['name'];
			if ($item['table_id'] != 0) $tmp['disabled'] = true;
			$res[] = $tmp;
		}
		die(json_encode($res));
	}
	
	public function table_info()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$id = isset($_POST['id']) ? $_POST['id'] : 0;
		if ($id > 0) {
			$this->conn->sql = "SELECT * FROM `table` WHERE `id`=$id";
			$res['code'] = 1;
			$res['msg'] = '成功';
			$res['data'] = $this->conn->row();
		}
		die(json_encode($res));
	}
	
	public function save_table()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$postData = isset($_POST['data']) ? $_POST['data'] : [];
		$id = isset($postData['id']) ? $postData['id'] : 0;
		$name = isset($postData['name']) ? $postData['name'] : '';
		$number = isset($postData['number']) ? $postData['number'] : 0;
		$status = isset($postData['status']) ? $postData['status'] : 0;
		if ($id > 0) {
			$this->conn->sql = "UPDATE `table` SET `name`='$name',`number`=$number,`status`=$status WHERE `id`=$id";
			$exec = $this->conn->execute();
			$res['code'] = $exec['code'];
			$res['msg'] = $exec['msg'];
		}
		die(json_encode($res));
	}
	
	public function change_table()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$time = time();
		$table_id = isset($_POST['table_id']) ? $_POST['table_id'] : 0;
		$user_id = isset($_POST['user_id']) ? $_POST['user_id'] : 0;
		$sqlstr = "UPDATE `user` SET `table_id`='$table_id',`edit_time`='$time' WHERE `id` IN ($user_id)";
		$this->conn->sql = $sqlstr;
		$exec = $this->conn->execute();
		$res['code'] = $exec['code'];
		$res['msg'] = $exec['msg'];
		die(json_encode($res));
	}
	
	public function sign_in()
	{
		$res = ['code' => 0, 'msg' => '失败'];
		$token = isset($_POST['token']) ? $_POST['token'] : '';
		
		if ( ! empty($token)) {
			$sqlstr1 = "SELECT * FROM `user` WHERE `token`='$token'";
			$this->conn->sql = $sqlstr1;
			$queryData = $this->conn->row();
			if ($queryData && count($queryData) > 0) {
				if ($queryData['status'] > 0) {
					$res['code'] = 2;
				} else {
					$time = time();
					$sqlstr2 = "UPDATE `user` SET `status`=1,`confirm_time`=$time WHERE `token`='$token'";
					$this->conn->sql = $sqlstr2;
					$exec = $this->conn->execute();
					if ($exec['code'] == 1) {
						$this->conn->sql = $sqlstr1;
						$queryData = $this->conn->row();
						$queryData['s_name'] = interceptString($queryData['name'], 25);
						$queryData['table_name'] = isset($queryData['table_id']) && $queryData['table_id'] == 0 ? '未安排' : getRow('table', ' and id=' . $queryData['table_id'])['name'];
						$res['data'] = $queryData;
						$res['code'] = $exec['code'];
						$res['msg'] = $exec['msg'];
					}
			
				}
			} else {
				$res['code'] = 3;
			}
			
		}
		die(json_encode($res));
	}
	
	public function signin_users()
	{
		$res = ['code' => 0, 'msg' => '失败', 'data' => []];
		$sqlstr = "SELECT * FROM `user` WHERE `status`='1' ORDER BY `confirm_time` DESC";
		$this->conn->sql = $sqlstr;
		$queryData = $this->conn->rows();
		if (count($queryData) > 0) {
			foreach ($queryData as $item) {
				$tmp = $item;
				$tmp['s_name'] = interceptString($item['name'], 25);
				$tmp['table_name'] = $item['table_id'] == 0 ? '未安排' : getRow('table', ' and id=' . $item['table_id'])['name'];
				$res['data'][] = $tmp;
			}
			$res['code'] = 1;
			$res['msg'] = '成功';
		}
		
		die(json_encode($res));
	}
}